package com.mapper;

import com.pojo.Carouse;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface CarouseMapper {
    /**
     * 查询所有启用的轮播图（按排序号降序）
     */
    @Select("SELECT id, img_url, title, sort, is_show FROM carousel WHERE is_show = 1 ORDER BY sort DESC")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "img_url", property = "imgUrl"),
            @Result(column = "title", property = "title"),
            @Result(column = "sort", property = "sort"),
            @Result(column = "is_show", property = "isShow")
    })
    List<Carouse> selectActiveCarousels();
    /**
     * 根据ID查询轮播图
     */
    @Select("SELECT id, img_url, title, sort, is_show FROM carousel WHERE id = #{id}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "img_url", property = "imgUrl"),
            @Result(column = "title", property = "title"),
            @Result(column = "sort", property = "sort"),
            @Result(column = "is_show", property = "isShow")
    })
    Carouse selectById(Long id);
    /**
     * 新增轮播图
     */
    @Insert("INSERT INTO carousel (img_url, title, sort, is_show) " +
            "VALUES (#{imgUrl}, #{title}, #{sort}, #{isShow})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Carouse carouse);
    /**
     * 更新轮播图
     */
    @Update("UPDATE carousel SET " +
            "img_url = #{imgUrl}, " +
            "title = #{title}, " +
            "sort = #{sort}, " +
            "is_show = #{isShow} " +
            "WHERE id = #{id}")
    int update(Carouse carouse);
    /**
     * 根据ID删除轮播图
     */
    @Delete("DELETE FROM carousel WHERE id = #{id}")
    int deleteById(Long id);
}
